package com.apps.ftapp.activity;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.ftapp.MainActivity;
import com.apps.ftapp.R;
import com.apps.ftapp.model.LoginModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.DensityUtils;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.apps.ftapp.util.StatusBarUtils;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

/**
 * 登录
 */
public class LoginActivity extends ABeseActivity implements View.OnClickListener {

    @BindView(R.id.mobile)
    EditText mobile;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.denglu)
    TextView denglu;
    @BindView(R.id.fpwd)
    TextView fpwd;
    @BindView(R.id.regist_btn)
    TextView regist_btn;

    @BindView(R.id.backBtn)
    ImageView back_btn;


    private final Gson gson = new Gson();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initEventAndData() {
        regist_btn.setOnClickListener(this);
        fpwd.setOnClickListener(this);
        denglu.setOnClickListener(this);
        back_btn.setOnClickListener(this);
        StatusBarUtil.setTranslucentForImageViewInFragment(LoginActivity.this, 0, null);
        StatusBarUtils.StatusBarLightMode(this);

        init();
    }

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        regist_btn.setOnClickListener(this);
        fpwd.setOnClickListener(this);
        denglu.setOnClickListener(this);
        back_btn.setOnClickListener(this);
        StatusBarUtil.setTranslucentForImageViewInFragment(LoginActivity.this, 0, null);
        StatusBarUtils.StatusBarLightMode(this);

        init();

        //判断是否登录过


    }*/

    private void init() {
        getAndroiodScreenProperty();
        ViewGroup.LayoutParams params = denglu.getLayoutParams();
        params.height = (int) (((DensityUtils.dp2px(this, (Constant.SCREEN_WIDTH - 120)) / 231.00)) * 38);
        denglu.setLayoutParams(params);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.regist_btn:
                ActivitySkipUtil.skipAnotherActivity(LoginActivity.this, RegistActivity.class, false);
                break;
            case R.id.fpwd:
                ActivitySkipUtil.skipAnotherActivity(LoginActivity.this, ForgetPwdActivity.class, false);
                break;
            case R.id.denglu:
                denglu();
                break;
            case R.id.backBtn:
                finish();
                break;
        }

    }

    private void denglu() {
        String username = mobile.getText().toString();
        String pwd = password.getText().toString();
        if ("".equals(username)) {
            Toasty.normal(this, "请输入手机号").show();
        } else {
            if ("".equals(pwd)) {
                Toasty.normal(this, "请输入密码").show();
            } else {

                RequestParams params = new RequestParams();
                params.put("mobile", username);
                params.put("password", pwd);
                Async.post("login", params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        if (statusCode == 200) {
                            L.i("main", new String(responseBody));

                            LoginModel result = gson.fromJson(new String(responseBody), LoginModel.class);
                            if (1 == Tools.isIntNull(result.getSuccess())) {

                                Toasty.normal(getApplicationContext(), "登录成功").show();
                                SharedPreferencesUtil.saveData(getApplicationContext(), "uid", result.getInfo().getUser().getId());
                                SharedPreferencesUtil.saveData(getApplicationContext(), "token", result.getInfo().getToken());
                                SharedPreferencesUtil.putBoolean(LoginActivity.this, SharedPreferencesUtil.IS_LOGIN, false);


                                Constant.token = result.getInfo().getToken();
                                Constant.uid = result.getInfo().getUser().getId();
                                Constant.nickname = result.getInfo().getUser().getNickname();

                                L.i("main", result.getInfo().getUser().getId());
                                L.i("token", "token:::" + result.getInfo().getToken());


                                ActivitySkipUtil.skipAnotherActivity(LoginActivity.this, MainActivity.class, true);


                            } else {
                                Toasty.normal(getApplicationContext(), result.getError()).show();
                            }

                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                    }
                });


            }
        }
    }

    public void getAndroiodScreenProperty() {
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;// 屏幕宽度（像素）
        int height = dm.heightPixels; // 屏幕高度（像素）
        float density = dm.density;//屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;//屏幕密度dpi（120 / 160 / 240）
        //屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);//屏幕宽度(dp)
        Constant.SCREEN_WIDTH = screenWidth;
        //int screenHeight = (int) (height / density);//屏幕高度(dp)
    }

}
